var app = getApp()
Page({

    data: {
        navtype: 1, // 当前选择tabbar
        page: 1, // 页码
        goodsList: [], // 商品列表

        currentTab: 0,
        data: '',

        noData: false, // 委托买 - 没有数据
        noMore: false, // 没有更多
        loading: false, // 加载中...
        hint: false, // 是否提示过

        overly: false,
        mask: false,
        dellid: ''

    },
    // formid
    formSubmit: function(e) {
        console.log(e.detail.formId)
        var formId = e.detail.formId

        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/user/collect_formid',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        formid: formId
                    },
                    success: function(res) {
                        console.log(formId)
                        wx.showToast({
                            title: formId,
                            icon: 'succes',
                            duration: 1000,
                            mask: true
                        })
                        if (res.data.code == 1) {


                        }

                    }
                })
            }
        })
    },
    // tabbar 切换
    selecte: function(e) {
        var type = e.target.dataset.type;
        this.setData({
            navtype: type,
            page: 1,
            noData:false,
            noMore:false,
            loading:false,
            hint:false,
            goodsList: []
        })
        this.getGoodsList()
    },

    // 获取商品数据
    getGoodsList(type) {
        var self = this;
        var page = this.data.page;
        var noData = this.data.noData;
        var datas = this.data.goodsList;
        var navtype = parseInt(this.data.navtype);
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/personal/mygoods',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        type: navtype,
                        page: page
                    },
                    success: function(res) {
                        var list = res.data;

                        if (list.data || datas.length) {
                            noData = false;
                        } else {
                            noData = true;
                        }
                        self.setData({
                            noData: noData
                        })
                        if (list.code == 1) {
                            if (list.data.data.length < 10) {
                                self.setData({
                                    noMore: true
                                })
                            }
                            self.setData({
                                goodsList: datas.concat(list.data.data),
                                loading: false
                            })
                        }
                        wx.hideLoading();
                    }
                })
            },
        })
    },
    onLoad: function(options) {
        this.dialog = this.selectComponent("#dialog");
        this.getGoodsList()
    },
    // 删除按钮
    deleteGoods: function(e) {
        var id = e.target.id;
        var type = e.target.dataset.type;
        this.setData({
            dellid: id
        });
        this.dialog.showDialog();
        return false;
    },
    // 加载更多
    loadingMore() {
        var page = ++this.data.page;
        var noMore = this.data.noMore;
        var loading = this.data.loading;

        if (!noMore && !loading) {
            this.setData({
                page: page,
                loading: true
            })
            wx.showLoading({
                title: '加载中'
            })
            this.getGoodsList();
        } else {
            if (!this.data.hint) {
                wx.showToast({
                    image: '../../img/warning.png',
                    title: '暂无更多',
                })
                this.setData({
                    hint: true
                })
            }
        }
    },
    // 取消按钮
    __cancelEvent() {
        this.dialog.hideDialog();
    },
    // 确认按钮
    __confirmEvent() {
        var self = this;
        var noData = this.data.noData;
        var goods_id = this.data.dellid;
        var goodsList = this.data.goodsList;
        // 删除订单
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/personal/del_goods',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id: goods_id
                    },
                    success: function(res) {
                        if (res.data.code == 1) {
                            goodsList.map((item, index) => {
                                if (goods_id == item.id) {
                                    goodsList.splice(index, 1)
                                }
                            })
                            if (goodsList.length) {
                                noData = false;
                            } else {
                                noData = true;
                            }
                            self.setData({
                                noData: noData,
                                goodsList: goodsList
                            })
                            self.dialog.hideDialog();
                            wx.showToast({
                                title: res.data.msg,
                            })
                        } else {
                            wx.showToast({
                                image: "../../img/warning.png",
                                title: res.data.msg,
                            })
                        }
                    }
                })
            }
        })
    }
})